package com.up.cloudfeiyibackend.controller;

import com.up.cloudfeiyibackend.constant.UserMessageConstant;
import com.up.cloudfeiyibackend.dto.UserPasswordDto;
import com.up.cloudfeiyibackend.dto.UserUpdateDto;
import com.up.cloudfeiyibackend.dto.UserLoginDTO;
import com.up.cloudfeiyibackend.result.Result;
import com.up.cloudfeiyibackend.service.UserService;
import com.up.cloudfeiyibackend.vo.UserVo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 用户相关接口
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    /**
     * 用户注册
     *
     * @param userLoginDTO
     * @return
     */
    @PostMapping("/register")
    public Result<Void> register(@RequestBody UserLoginDTO userLoginDTO) {
        userService.register(userLoginDTO);
        return Result.success(UserMessageConstant.REGISTER_SUCCESS);
    }

    /**
     * 用户登录
     *
     * @param userLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<String> login(@RequestBody UserLoginDTO userLoginDTO) {
        String token = userService.login(userLoginDTO);
        return Result.success(UserMessageConstant.LOGIN_SUCCESS, token);
    }

    /**
     * 获取当前用户信息
     *
     * @return
     */
    @GetMapping("/me")
    public Result<UserVo> getCurrentUserInfo() {
        UserVo userVo = userService.getCurrentUserInfo();
        return Result.success(UserMessageConstant.FIND_ME_SUCCESS,userVo);
    }

    /**
     * 修改当前用户信息
     *
     * @param userUpdateDto
     * @return
     */
    @PutMapping("/me")
    public Result<UserVo> updateCurrentUserInfo(@RequestBody UserUpdateDto userUpdateDto) {
        UserVo userVo = userService.updateCurrentUserInfo(userUpdateDto);
        return Result.success(UserMessageConstant.UPDATE_ME_SUCCESS, userVo);
    }

    /**
     * 修改当前用户密码
     *
     * @param userPasswordDto
     * @return
     */
    @PutMapping("/password")
    public Result<Void> updatePassword(@RequestBody UserPasswordDto userPasswordDto) {
        userService.updatePassword(userPasswordDto);
        return Result.success(UserMessageConstant.UPDATE_PASSWORD_SUCCESS);
    }
}
